<?php
class ForumMembers extends CActiveRecord {
    public $verifyCode;
	const REGEXP_PASSWORD = '/^[0-9a-zA-Z]{6,20}$/';
	public $confirmPassword;
	public $oldPassword;

	public $isSelf;

	public static function model($className = __CLASS__) {
		return parent::model($className);
	}
	
	public function rules() {
		return array(
			array('password', 'compare', 'on' => array('resetPassword', 'changePassword'), 'compareAttribute' => 'confirmPassword', 'message' => '两次输入密码不一致，请重新输入。'),
		);
	}
	
	public function relations() {
		return array(
			'admin' => array(self::HAS_ONE, 'Admins', 'uid'),
			'bbs' => array(self::HAS_ONE, 'BbsMembers', 'uid'),
			'bbsMemberFields' => array(self::HAS_ONE, 'BbsMemberFields', 'uid'),
			'psychologist' => array(self::HAS_ONE, 'Psychologists', 'userId')
		);
	}
	
	public function tableName() {
		return 'uc_members';
	}

    public function userName() {
        return $this->username;
    }

    public function convertUserNameEncoding() {
//    		echo $this->username;echo mb_detect_encoding($this->username);
    	if(UC_DBCHARSET == 'gbk' && mb_detect_encoding($this->username) == 'UTF-8') {
    		return iconv('UTF-8', 'GBK', $this->username);
    	} else if(UC_DBCHARSET == 'utf8' && mb_detect_encoding($this->username) == 'GBK') {
    		return iconv('GBK', 'UTF-8', $this->username);
//    		$this->username = mb_convert_encoding($user->username, 'UTF-8', 'GBK');
    	}
//    		echo $this->username;echo mb_detect_encoding($this->username);die;
    	return $this->username;
    }

	public function findByUserName($userName) {
        return self::model()->find('username = ?', array($userName));
	}
    
    public function setSecurePassword($password) {
        return $this->password = md5(md5($password).$this->salt);
    }

    public function findByEmail($email) {
       return self::model()->find('email = ?', array($email));
    }

	public function getIsSelf() {
		return Yii::app()->user->id == $this->uid;
	}

	public static function isLogin($redirect = false) {
		$result = isset(Yii::app()->user->id) ? Yii::app()->user->id : false;
		if($redirect === true && ! $result) {
			Yii::app()->loginRequired();
		}
		return $result;
	}
	
	public function getForgotPasswordUrl() {
		if(($c = Yii::app()->getController()) !== null) {
			return $c->createAbsoluteUrl('/users/resetpassword', array( 
					'encoded' => md5($this->password),
					'email' => urlencode($this->email),
					'username' => urlencode($this->username)
				)
			);
		}
		return false;
	}
	
	public function getRegisterMailText() {
		if(($c = Yii::app()->getController()) !== null) {
			$fogotPasswordUrl = $c->createAbsoluteUrl('/users/fogotpassword');
			$loginUrl = $c->createAbsoluteUrl('/users/login');
			$baseUrl = BASE_URL;
			return '
您好，'.$this->username.'，<br />
<br />
感谢您注册成为广州心理咨询网的会员，愿您在这里获取所需！<br />
<br />
您的用户名是: '.$this->username.'<br />
注册邮箱是: '.$this->email.'<br />
登录本站请点击<a href="'.$loginUrl.'" title="登录">这里</a>。<br />
<br />
建议您保管好本邮件！如忘记密码，请打开这个地址找回密码: '.$fogotPasswordUrl.'<br />
即刻起您就可以享受广州心理咨询网为您带来的全新全面的心理健康资讯！<br />
<br />
如果您有任何疑问欢迎联系我们: <br />
网址: '.$baseUrl.'<br />
咨询热线: 020-62728787，83644639<br />
邮件: huiyn2009@163.com<br />
<br />
广州心理咨询网';
		}
		return '';
	}

	public function getFogotPasswordMailText() {
		if(($c = Yii::app()->getController()) !== null) {
			$fogotPasswordUrl = $this->getForgotPasswordUrl();
			$baseUrl = BASE_URL;
			$_username = $this->username;
			return '您好，'.$_username.'，<br />'."
<br />
您在【广州心理咨询网】提交了“找回密码”的信息，这是一封密码重置确认邮件。如果您并未尝试修改密码，请忽略本邮件。 <br />
<br />
密码重置:<br />
您可以通过点击以下链接重置帐户密码，请将该链接复制到浏览器地址栏中访问: <br />
$fogotPasswordUrl
<br /><br />
如果您有任何疑问欢迎联系我们: <br />
网址: $baseUrl<br />
客服热线: 020-83644639<br />
邮件: huiyn2009@163.com<br />
<br />
广州心理咨询网";
		}
		return '';
	}
	
	public function getAvatarUrl($size = 'middle', $type = 'virtual') {
		if(defined('UC_API')) {
			return UC_API.'/avatar.php?uid='.$this->uid.'&type='.$type.'&size='.$size;
		}
		return '';
	}
	
	public function getIsAdmin() {
		return isset(ForumMembers::model()->findByPk($this->uid)->admin) &&
				 ForumMembers::model()->findByPk($this->uid)->admin !== null;
	}
	
	static public function isAdmin() {
		if($userId = Yii::app()->user->id) {
			return isset(ForumMembers::model()->findByPk($userId)->admin) &&
				 ForumMembers::model()->findByPk($userId)->admin !== null;
		}
		return false;
	}
}